﻿@page "/Production/Archives"
@using BootstrapAdmin.DataAccess.Models
@using BootstrapAdmin.Web.Core.Services.Production;
@using BootstrapAdmin.Web.ViewModels.Production;
@inject IArchiveInfoService archiveInfoService

<AdminTable TItem="Archive" IsMultipleSelect="false" IsPagination="true" PageItemsSource="@PageItemsSource"
    ShowDefaultButtons="false" ShowExtendButtons="false"
    ShowSearch="true" SearchMode="SearchMode.Top" CustomerSearchModel="@ArchiveInfoSearchModel" ShowExportButton="true"
    OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.SN" Width="180"></TableColumn>
        <TableColumn @bind-Field="@context.Station"></TableColumn>
        <TableColumn @bind-Field="@context.OrderNo"></TableColumn>
        <TableColumn @bind-Field="@context.ProductCode"></TableColumn>
        <TableColumn @bind-Field="@context.Pass">
            <Template Context="v">
                <span>@(v.Value == 1 ? "OK" : "NG")</span>
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Cycletime"></TableColumn>
        <TableColumn @bind-Field="@context.Operator"></TableColumn>
        <TableColumn @bind-Field="@context.Shift"></TableColumn>
        <TableColumn @bind-Field="@context.Pallet"></TableColumn>
        <TableColumn @bind-Field="@context.CreatedAt"></TableColumn>
    </TableColumns>
    <DetailRowTemplate>
        @{
            var DetailDataSource = archiveInfoService.GetArchiveItems(context.Id);
        }
        <Table TItem="ArchiveItem" IsBordered="true" ShowToolbar="false" Items="@DetailDataSource">
            <TableColumns Context="Detail">
                <TableColumn @bind-Field="@Detail.Name" Width="180" />
                <TableColumn @bind-Field="@Detail.Value" />
            </TableColumns>
        </Table>
    </DetailRowTemplate>
    <CustomerSearchTemplate>
        @if (context is ArchiveInfoSearchModel searchModel)
        {
            <div class="row g-3 form-inline">
                <div class="col-sm-6 col-md-6">
                    <BootstrapInput @bind-Value="searchModel.Station" ShowLabel="true" />
                </div>
                <div class="col-sm-6 col-md-6">
                    <BootstrapInput @bind-Value="@searchModel.SN" ShowLabel="true" />
                </div>
               <div class="col-sm-6 col-md-6">
                    <DateTimeRange @bind-Value="@searchModel.CreatedTime" ShowLabel="true" AllowNull="true" />
                </div>
            </div>
        }
    </CustomerSearchTemplate>
</AdminTable>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 20, 50, 100 };

    private ArchiveInfoSearchModel ArchiveInfoSearchModel = new();

    private async Task<QueryData<Archive>> OnQueryAsync(QueryPageOptions options)
    {
        var filter = new ArchiveInfoFilter
        {
            Station = ArchiveInfoSearchModel.Station,
            SN = ArchiveInfoSearchModel.SN,
            CreatedTimeStart = ArchiveInfoSearchModel.CreatedTime?.Start,
            CreatedTimeEnd = ArchiveInfoSearchModel.CreatedTime?.End,
        };

        var (items, count) = await archiveInfoService.GetPagedAsync(filter, options.PageIndex, options.PageItems);

        return new QueryData<Archive>()
        {
            Items = items,
            TotalCount = count,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true,
            IsAdvanceSearch = true,
        };
    }
}
